package cn.lunkr.webview_sync_cookies.view;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.lunkr.webview_sync_cookies.util.SystemUtil;

/**
 * Created by wb on 2016/1/8.
 */
public class SurpriseLayout extends LinearLayout {

    private View backLayout;

    /**
     * web控件
     */
    private WebView webView;

    /**
     * 进度条
     */
    private TextView progressTv;

    private TextView titleTv;

    private String mTitleStr;

    private ChangeTitle mChangeTitle;

    public void setListener(SurpriseListener mListener) {
        this.mListener = mListener;
    }

    private SurpriseListener mListener;

    public void setChangeTitle(ChangeTitle mChangeTitle) {
        this.mChangeTitle = mChangeTitle;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SurpriseLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public SurpriseLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SurpriseLayout(Context context) {
        super(context);
        init(context);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void init(Context context) {
//        setBackgroundResource(R.color.main_bg_color);
        setOrientation(VERTICAL);

        //标题栏相亲
//        View headView = LayoutInflater.from(context)
//                .inflate(R.layout.head_controll_layout, null, false);
//        LayoutParams titleParams = new LayoutParams(LayoutParams.MATCH_PARENT,
//                LayoutParams.WRAP_CONTENT);
//        headView.setLayoutParams(titleParams);
//        titleTv = (TextView) headView.findViewById(R.id.title_center_tv);
//        titleTv.setText("惊喜");
//        titleTv.setVisibility(View.VISIBLE);
//        backLayout = headView.findViewById(R.id.title_left_btn);
//        backLayout.setVisibility(View.VISIBLE);
//        ImageView leftIv = (ImageView) headView.findViewById(R.id.left_img);
//        leftIv.setVisibility(View.VISIBLE);
//        leftIv.setImageResource(R.drawable.ic_back);
//        addView(headView);

        //网页布局
        FrameLayout bodyFl = new FrameLayout(context);
        LayoutParams lpBody = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
        lpBody.weight = 1;
        bodyFl.setLayoutParams(lpBody);
        addView(bodyFl);

        RelativeLayout llBody = new RelativeLayout(context);
        llBody.setLayoutParams(new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        bodyFl.addView(llBody);

        progressTv = new TextView(context);
        progressTv.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                5));
        progressTv.setBackgroundColor(0xFF33FF33);
        progressTv.setVisibility(GONE);

        webView = new WebView(context);
        RelativeLayout.LayoutParams webParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        webView.setLayoutParams(webParams);

        webView.setHorizontalScrollBarEnabled(true);
        webView.setVerticalScrollBarEnabled(false);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setSaveFormData(false);
        webView.getSettings().setSavePassword(false);
        webView.getSettings().setSupportZoom(false);
//        webView.getSettings().setSupportZoom(true);// 设置可以支持缩放
//        webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);  //设置默认缩放方式尺寸是far
//        webView.getSettings().setBuiltInZoomControls(true);  //设置出现缩放工具

        webView.getSettings().setPluginState(PluginState.ON); // webView无法播放视频 用该属性
//        webView.getSettings().setPluginsEnabled(true);
//        webView.getSettings().setLoadWithOverviewMode(true); //无法根据浏览器居中显示内容这个问题只要设置下面两个熟悉即可
//        webView.getSettings().setUseWideViewPort(true);//无法根据浏览器居中显示内容这个问题只要设置下面两个熟悉即可
//        webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); //让网页自适应屏幕宽度

//        webView.getSettings()
//                .setUserAgentString("Mozilla/5.0 (Android; iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10");

        final int sWidth = SystemUtil.getWidth(context);

        WebChromeClient client = new WebChromeClient() {

            @Override
            public void onCloseWindow(WebView window) {
                super.onCloseWindow(window);
                if (mListener != null) {
                    mListener.closeWindow();
                }
            }

            //处理加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                RelativeLayout.LayoutParams lpProgress = (RelativeLayout.LayoutParams) progressTv.getLayoutParams();
                lpProgress.width = sWidth * newProgress / 100;
                progressTv.setLayoutParams(lpProgress);
                String title = webView.getTitle() != null ? webView.getTitle().trim() : "";
                if (!TextUtils.isEmpty(title)) {
                    if (!title.equals(mTitleStr)) {
                        mTitleStr = title;
                        if (mChangeTitle != null) {
                            mChangeTitle.onChangeTitle(mTitleStr);
                        }
                    }
                }
                if (newProgress > 0 && newProgress < 100) {
                    progressTv.setVisibility(VISIBLE);
                } else {
                    progressTv.setVisibility(GONE);
                }
            }
        };
        webView.setWebChromeClient(client);

        llBody.addView(webView);
        llBody.addView(progressTv);

        LinearLayout llShadow = new LinearLayout(context);
//        llShadow.setBackgroundResource(R.color.main_bg_color);
        llShadow.setLayoutParams(new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        bodyFl.addView(llShadow);
    }


    public View getBackLayout() {
        return backLayout;
    }

    public WebView getWebView() {
        return webView;
    }

    public TextView getProgressTv() {
        return progressTv;
    }

    public TextView getTitleTv() {
        return titleTv;
    }

    public interface ChangeTitle {
        void onChangeTitle(String title);
    }

    public interface SurpriseListener{

        void closeWindow();
    }
}
